<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: categories.helper.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

class categoriesHelper {
	var $_db;

	public function  __construct() {
		global $db;

		$this->_db = $db;
	}

	public function multi_copy_to($cat_ids = '', $pro_ids = '', $cPath = '', $pID = '') {
		
		require_once ('includes/classes/class.categories.php');
		$catfunc = new categories($this->_db);

		$content = draw_form('multi_action_form', FILENAME_CATEGORIES, 'action=multi_action_confirm&cPath='.$cPath, 'post', 'id="dialog_form"').draw_hidden_field('multi_copy_confirm','').draw_hidden_field('products_id', $pID);

		$content .= '<table width="100%" cellpadding="8"><tr><td colspan="3">';

		if(is_array($cat_ids)) {
            foreach ($cat_ids AS $key => $multi_category) {
                $category = $this->_db->db_query("SELECT
                										c.categories_id,
                										cd.categories_name,
                										c.categories_image,
                										c.parent_id,
                										c.sort_order,
                										c.date_added,
                										c.last_modified,
                										c.categories_status
                									FROM
                										".TABLE_CATEGORIES." c,
                										".TABLE_CATEGORIES_DESCRIPTION." cd
                									WHERE
                										c.categories_id = '".$multi_category."'
                									AND
                										c.categories_id = cd.categories_id
                									AND
                										cd.language_id = '".(int)$_SESSION['languages_id']."'");

                $category_childs   = array('childs_count'   => $catfunc->count_category_childs($multi_category));
                $category_products = array('products_count' => $catfunc->count_category_products($multi_category, true));
                $cInfo_array = array_merge($category->fields, $category_childs, $category_products);
                $cInfo = new objectInfo($cInfo_array);
                $content .= '<b>'.$cInfo->categories_name.'</b><br />';
                if ($cInfo->childs_count > 0)
                	$content .= sprintf(TEXT_MOVE_WARNING_CHILDS, $cInfo->childs_count).'<br />';
                if ($cInfo->products_count > 0)
                	$content .= sprintf(TEXT_MOVE_WARNING_PRODUCTS, $cInfo->products_count).'<br />';

                $content .= '<input type="hidden" value="'.$multi_category.'" name="multi_categories[]" />'."\n";
                $content .= '<div class="line"></div>';
            }
        }

        if(is_array($pro_ids)) {
            foreach($pro_ids AS $key => $multi_product) {
                $content .= '<b>'.get_products_name($multi_product).'</b><br />';
                $product_categories_string = '';
                $product_categories = output_generated_category_path($multi_product, 'product');
                $product_categories_string = $product_categories.'<br />';
                $content .= $product_categories_string;
                $content .= '<input type="hidden" value="'.$multi_product.'" name="multi_products[]" />'."\n";
            }
        }

		$content .= '</td></tr><tr><td width="48%" valign="top">';
		if (QUICKLINK_ACTIVATED=='true') {
	        $content .= '<b>'.TEXT_MULTICOPY.'</b><br /><div class="line"></div>';
	        $cat_tree = get_category_tree();
	        $tree='';
	        for ($i=0;$n=sizeof($cat_tree),$i<$n;$i++)
		      $content .= '<input type="checkbox" name="dest_cat_ids[]" value="'.$cat_tree[$i]['id'].'" /> '.$cat_tree[$i]['text'].'<br />';

        }
        $content .= '</td><td width="2%" valign="middle"><b><em>oder</em></b></td><td width="49%" valign="top">';
        $content .= '<b>'.TEXT_SINGLECOPY.'</b><br /><div class="line"></div>';
        $content .= draw_pull_down_menu('dest_category_id', get_category_tree(), $cPath);
        $content .= '<table cellpadding="10"><tr><td valign="top">'.TEXT_HOW_TO_COPY.'</td><td valign="top">'.draw_radio_field('copy_as', 'duplicate', true).' '.TEXT_COPY_AS_DUPLICATE.'<br />'.draw_radio_field('copy_as', 'link').' '.TEXT_COPY_AS_LINK.'</td></tr></table>';

        $content .= '</td></tr></table>';

		$content .= '</form>';

        return $content;
	}

	public function multi_move_to($cat_ids = '', $pro_ids = '', $cPath = '') {
		
		require_once('includes/classes/class.categories.php');
		$catfunc = new categories($this->_db);

		$content 	= draw_form('multi_action_form', FILENAME_CATEGORIES, 'action=multi_action_confirm&cPath='.$cPath, 'post', 'id="dialog_form"').draw_hidden_field('multi_move_confirm','').draw_hidden_field('products_id', $pID);
		$content .= '<table width="100%" cellpadding="4"><tr><td colspan="2">';

		if(is_array($cat_ids)) {
            foreach ($cat_ids AS $key => $multi_category) {
                $category = $this->_db->db_query("SELECT 
                										c.categories_id, 
                										cd.categories_name, 
                										c.categories_image, 
                										c.parent_id, 
                										c.sort_order, 
                										c.date_added, 
                										c.last_modified, 
                										c.categories_status 
                									FROM 
                										".TABLE_CATEGORIES." c, 
                										".TABLE_CATEGORIES_DESCRIPTION." cd 
                									WHERE 
                										c.categories_id = '".$multi_category."' 
                									AND 
                										c.categories_id = cd.categories_id 
                									AND 
                										cd.language_id = '".(int)$_SESSION['languages_id']."'");
                $category_childs   = array('childs_count'   => $catfunc->count_category_childs($multi_category));
                $category_products = array('products_count' => $catfunc->count_category_products($multi_category, true));
                $cInfo_array = array_merge($category->fields, $category_childs, $category_products);
                $cInfo = new objectInfo($cInfo_array);
                $content .= '<b>'.$cInfo->categories_name.'</b><br />';
                if ($cInfo->childs_count > 0)
                	$content .= sprintf(TEXT_MOVE_WARNING_CHILDS, $cInfo->childs_count).'<br />';
                if ($cInfo->products_count > 0)
                	$content .= sprintf(TEXT_MOVE_WARNING_PRODUCTS, $cInfo->products_count).'<br />';

                $content .= '<input type="hidden" value="'.$multi_category.'" name="multi_categories[]" />'."\n";
                $content .= '<div class="line"></div>';
            }
        }

        if(is_array($pro_ids)) {
            foreach($pro_ids AS $key => $multi_product) {
                $content .= '<b>'.get_products_name($multi_product).'</b><br />';
                $product_categories_string = '';
                $product_categories = output_generated_category_path($multi_product, 'product');
                $product_categories_string = $product_categories.'<br />';
                $content .= $product_categories_string;
                $content .= '<input type="hidden" value="'.$multi_product.'" name="multi_products[]" />'."\n";
            }
        }

		$content .= '</td></tr><tr><td >';

        $content .= '<b>'.TEXT_MOVE_ALL.'</b><br />'.draw_pull_down_menu('move_to_category_id', get_category_tree(), $cPath).'<br />
        			<input type="hidden" name="src_category_id" value="'.$cPath.'" />';

        $content .= '</td></tr></table>';

		$content .= '</form>';

        return $content;
	}

	public function multi_delete($cat_ids = '', $pro_ids = '', $cPath = '') {
		
		require_once('includes/classes/class.categories.php');
		$catfunc = new categories($this->_db);

		$content  = draw_form('multi_action_form', FILENAME_CATEGORIES, 'action=multi_action_confirm&cPath='.$cPath, 'post', 'id="dialog_form"').draw_hidden_field('multi_delete_confirm','').draw_hidden_field('products_id', $pID);
		$content .= '<table width="100%" cellpadding="4"><tr><td colspan="2">';

		if(is_array($cat_ids)) {
            foreach($cat_ids AS $key => $multi_category) {
                $category = $this->_db->db_query("SELECT
	                									c.categories_id,
	                									cd.categories_name,
	                									c.categories_image,
	                									c.parent_id,
	                									c.sort_order,
	                									c.date_added,
	                									c.last_modified,
	                									c.categories_status
	                								FROM
	                									".TABLE_CATEGORIES." c,
	                									".TABLE_CATEGORIES_DESCRIPTION." cd
	                								WHERE
	                									c.categories_id = '".$multi_category."'
	                								AND
	                									c.categories_id = cd.categories_id
	                								AND
	                									cd.language_id = '".(int)$_SESSION['languages_id']."'");

                $category_childs   = array('childs_count' => $catfunc->count_category_childs($multi_category));
                $category_products = array('products_count' => $catfunc->count_category_products($multi_category, true));
                $cInfo_array = array_merge($category->fields, $category_childs, $category_products);
                $cInfo = new objectInfo($cInfo_array);
                $content .= '<b>'.$cInfo->categories_name.'</b><br />';
                if ($cInfo->childs_count > 0)
                	$content .= sprintf(TEXT_DELETE_WARNING_CHILDS, $cInfo->childs_count).'<br />';
                if ($cInfo->products_count > 0)
                	$content .= sprintf(TEXT_DELETE_WARNING_CHILDS, $cInfo->products_count).'<br />';

                $content .= '<input type="hidden" value="'.$multi_category.'" name="multi_categories[]" />'."\n";
                $content .= '<div class="line"></div>';
            }
        }

        if(is_array($pro_ids)) {
            foreach($pro_ids AS $key => $multi_product) {
                $content .= '<b>'.get_products_name($multi_product).'</b><br />';
                $product_categories_string = '';
                $product_categories = generate_category_path($multi_product, 'product');
				for ($i = 0, $n = sizeof($product_categories); $i < $n; $i++) {
					$category_path = '';
					for ($j = 0, $k = sizeof($product_categories[$i]); $j < $k; $j++) {
						$category_path .= $product_categories[$i][$j]['text'].'&nbsp;&gt;&nbsp;';
					}
					$category_path = substr($category_path, 0, -16);
					$product_categories_string .= draw_checkbox_field('multi_products_categories['.$multi_product.'][]', $product_categories[$i][sizeof($product_categories[$i])-1]['id'], true).'&nbsp;'.$category_path.'<br />';
				}
                $product_categories_string = $product_categories_string.'<br />';
                $content .= $product_categories_string;
                $content .= '<input type="hidden" value="'.$multi_product.'" name="multi_products[]" />'."\n";
            }
        }
		$content .= '</td></tr></table>';
		$content .= '</form>';

        return $content;
	}
}